package sms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import sms.entity.Class;
import sms.entity.Course;
import sms.entity.User;
import sms.service.ClassService;
import sms.service.CourseService;
import sms.service.UserService;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;

    @Override
    public void addClass(Class clazz) {
        String sql="insert into class values(?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,clazz.getClassId(),clazz.getCourseId(),clazz.getClassType(),clazz.getTeacherId(),clazz.getStartDate(),clazz.getEndDate(),clazz.getScoreSubmitDeadline());
    }

    @Override
    public void editClass(Class clazz) {
        String sql="update class set course_id=?,class_type=?,teacher_id=?,start_date=?,end_date=?,score_submit_deadline=? where class_id=?";
        jdbcTemplate.update(sql,clazz.getCourseId(),clazz.getClassType(),clazz.getTeacherId(),clazz.getStartDate(),clazz.getEndDate(),clazz.getScoreSubmitDeadline(),clazz.getClassId());
    }

    @Override
    public List<Class> getClasses(String id) {
        String sql;
        List<Object> params = new ArrayList<>();

        if (id != null && !id.trim().isEmpty()) {
            // 如果提供了班级ID，则按ID精确查询
            sql = "select * from class where class_id = ?";
            params.add(id);
        } else {
            // 如果未提供ID，则查询所有班级
            sql = "select * from class";
        }
        return jdbcTemplate.query(sql,params.toArray(), new RowMapper<Class>() {
            @Override
            public Class mapRow(ResultSet rs, int rowNum) throws SQLException {
                Class clazz = new Class();
                clazz.setClassId(rs.getString("class_id"));
                clazz.setCourseId(rs.getString("course_id"));
                clazz.setClassType(rs.getString("class_type"));
                clazz.setTeacherId(rs.getInt("teacher_id"));
                clazz.setStartDate(rs.getDate("start_date"));
                clazz.setEndDate(rs.getDate("end_date"));
                clazz.setScoreSubmitDeadline(rs.getDate("score_submit_deadline"));
                Course course=courseService.getCourse(rs.getString("course_id"));
                clazz.setCourse(course);
                User teacher=userService.getUser(rs.getInt("teacher_id"));
                clazz.setTeacher(teacher);
                return clazz;
            }
        });
    }

    @Override
    public void deleteClass(String id) {
        String sql="delete from class where class_id=?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public List<Class> getClasses(Date startDate, Date endDate) {
        String sql="select * from class where start_date>=? and end_date<=?";
        List<Object> params = new ArrayList<>();
        params.add(startDate);
        params.add(endDate);
        return jdbcTemplate.query(sql, params.toArray(), new RowMapper<Class>() {
            @Override
            public Class mapRow(ResultSet rs, int rowNum) throws SQLException {
                Class clazz = new Class();
                clazz.setClassId(rs.getString("class_id"));
                clazz.setCourseId(rs.getString("course_id"));
                clazz.setClassType(rs.getString("class_type"));
                clazz.setTeacherId(rs.getInt("teacher_id"));
                clazz.setStartDate(rs.getDate("start_date"));
                clazz.setEndDate(rs.getDate("end_date"));
                clazz.setScoreSubmitDeadline(rs.getDate("score_submit_deadline"));
                Course course=courseService.getCourse(rs.getString("course_id"));
                clazz.setCourse(course);
                User teacher=userService.getUser(rs.getInt("teacher_id"));
                clazz.setTeacher(teacher);
                return clazz;
            }
        });
    }
}